package com.hxkj.service.article.impl;


import com.hxkj.client.service.AdvertisementServiceClient;
import com.hxkj.domain.po.Advertisement;
import com.hxkj.service.article.AdvertisementService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 广告管理
 * chenfangzhou 2018/09/14
 */
@Service
@Slf4j
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private AdvertisementServiceClient advertisementServiceClient;

    /**
     * 获取单条广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<Advertisement> get(Advertisement advertisement) throws Exception {
        try {
            if (advertisement.getAdvertisementId() == null) throw new CustomMessageException(401, "广告id为空!");
            advertisement.setDelTag(0);
            ResultDataStruct<Advertisement> result = advertisementServiceClient.get(advertisement);
            if (result.getCode() == 200) {
                return result;
            } else {
                result.setCode(401);
                result.setMessage("获取广告信息失败!");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 保存广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct save(Advertisement advertisement) throws Exception {
        try {
            if (StringUtils.isBlank(advertisement.getName())) throw new CustomMessageException(401, "广告名称为空!");
            if (StringUtils.isBlank(advertisement.getUrlPath())) throw new CustomMessageException(402, "广告url地址为空!");
            if (advertisement.getSiteId() == null) throw new CustomMessageException(403, "站点id为空!");
            //判断广告名称是否重复
            List<Advertisement> list = advertisementServiceClient.getList(advertisement);
            if (list.size() > 0) throw new CustomMessageException(404, "广告已存在,请勿重复录入!");
            ResultMessageStruct result = advertisementServiceClient.save(advertisement);
            if (result.getCode() == 200) {
                return result;
            } else {
                result.setCode(405);
                result.setMessage("保存广告信息失败!");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 修改广告
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct modify(Advertisement advertisement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            if (advertisement != null && advertisement.getAdvertisementId() == null && StringUtils.isBlank(advertisement.getAdvertisementIds()))
                throw new CustomMessageException(401, "广告id为空!");
            result = advertisementServiceClient.modify(advertisement);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomMessageException(result.getCode(), result.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 删除广告,软删除
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct remove(Advertisement advertisement) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            if (advertisement.getAdvertisementId() == null && StringUtils.isBlank(advertisement.getAdvertisementIds()))
                throw new CustomMessageException(401, "广告id为空!");
            advertisement.setDelTag(1);
            result = advertisementServiceClient.modify(advertisement);
            if (result.getCode() == 200) {
                return result;
            } else {
                result.setCode(402);
                result.setMessage("删除广告失败!");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 获取广告列表,带分页
     * @param advertisement
     * @param pageTo
     * @return
     * @throws Exception
     */
    @Override
    public ResultPageStruct<Advertisement> getListWithPage(Advertisement advertisement, PageTo pageTo) throws Exception {
        try {
            if (pageTo == null || pageTo.getPageNum() == null || pageTo.getPageSize() == null) {
                pageTo.setPageNum(1);
                pageTo.setPageSize(10);
            }
            advertisement.setDelTag(0);
            ParameterDataStruct<Advertisement> param = new ParameterDataStruct<>();
            param.setData(advertisement);
            param.setPageTo(pageTo);

            return advertisementServiceClient.getListWithPage(param);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 获取广告类表,不带分页
     * @param advertisement
     * @return
     * @throws Exception
     */
    @Override
    public List<Advertisement> getList(Advertisement advertisement) throws Exception {
        try {
            if (advertisement == null) advertisement = new Advertisement();
            advertisement.setDelTag(0);
            List<Advertisement> artList = advertisementServiceClient.getList(advertisement);
            return artList;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 修改广告的排序
     * @param map
     * @throws Exception
     */
    @Override
    public void modifySortNum(HashMap<String, Object> map) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (map.get("siteId") == null) {
                throw new CustomMessageException(401, "站点id为空!");
            }
            resultMessageStruct = advertisementServiceClient.modifySortNum(map);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
